Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multipart/form-data support for typespec-todo #2984

Merged
merged 10 commits into from
Jan 15, 2025
Merged

Conversation

timovv
Copy link
Member

@timovv timovv commented Jan 9, 2025

multipart/form-data support for the cases covered by the todo spec for the e2e demo.

Includes basic support for file uploads, JSON, and text parts. There are some items to follow up on after this PR:

  • Anonymous models with @multipartBody are not currently supported
  • Models with text parts and a custom content type header (the non-string cadl ranch case) are not yet supported

Fixes #2422

@timovv timovv force-pushed the multipart-todo branch 2 times, most recently from c2e63bd to 8c0bd25 Compare January 9, 2025 21:53
@@ -646,6 +646,9 @@ export interface FileContentResponse {
content: Uint8Array;
}

// @public
export type FileContents = string | NodeJS.ReadableStream | ReadableStream<Uint8Array> | Uint8Array | Blob;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious why this is added? is this expected?

Copy link
Member Author

@timovv timovv Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this spec has an anonymous model for MFD which isn't properly supported yet. FileContents should be used in uploadFile but is currently Uint8Array. Will fix.

Copy link
Member Author

@timovv timovv Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address anonymous models including this case in a follow up issue


```tsp
model RequestBody {
files: HttpPart<File>[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a case for

model RequestBody {
  files: HttpPart<File[]>;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that case really makes sense. That body model would represent a single part consisting of a JSON array of File objects.

We do already have a case for a JSON array here.

@@ -128,6 +130,9 @@ function emitType(context: SdkContext, type: SdkType, sourceFile: SourceFile) {
if (isAzureCoreErrorType(context.program, type.__raw)) {
return;
}
if (isOrExtendsHttpFile(context.program, type.__raw!)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use usage to filter the HttpPart models out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't because tcgc doesn't set UsageFlags.MultipartFormData for Http.File. It only does that for the actual multipart body model.

);
if (
!properties.some((x) => x.name === name) &&
!(existInterface && existInterface.getProperty(name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we need this filter? do you see any cases where there's duplication?

Copy link
Member Author

@timovv timovv Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the multipart payload cadl ranch spec causes this duplication for some reason. If you revert this change and regenerate you should see the issue.

NameType.Property
);

// HACK: check if the statement includes a group of this name already to prevent an operation group appearing multiple times
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it's strange if an operation group would appear multiple times. how could I repro?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above -- without this the multipart spec generates the group 3 times. I think it might have to do with the way that the operations are nested?

@timovv timovv changed the title [WIP] multipart/form-data support for typespec-todo multipart/form-data support for typespec-todo Jan 14, 2025
@timovv timovv marked this pull request as ready for review January 14, 2025 21:56
Copy link
Member

@qiaozha qiaozha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

parts.push(partDefinition);
}

// TODO: How to handle additionalProperties for MFD?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an issue to track this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it to the follow up issue #3001

@timovv timovv merged commit d6ccc49 into Azure:main Jan 15, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multipart Form Data Implementation in Modular
4 participants